#include "print.h"
#include <QDateTime>
#include <QTime>
#include <QTimer>
#include <QDebug>
#include <QCoreApplication>
#include <QMessageBox>
#include <QString>
#include <QByteArray>

PRINT::PRINT(QWidget *parent) :
    QMainWindow(parent)
{
    openserial();
}

void PRINT::openserial()
{
    serial = new QSerialPort(this);
#ifndef i386
    QString portName="/dev/ttySAC1";
#else
    QString portName="/dev/ttyUSB0";
#endif
    serial->setPortName(portName);
    serial->setBaudRate(QSerialPort::Baud9600);
    serial->setDataBits(QSerialPort::Data8);
    serial->setParity(QSerialPort::NoParity);
    serial->setStopBits(QSerialPort::OneStop);
    serial->setFlowControl(QSerialPort::NoFlowControl);
    serial->open(QIODevice::ReadWrite);

    if(serial->isOpen())
           qDebug()<<"iiii";
    else
        QMessageBox::warning(this,tr("错误"),tr("连接到打印机失败！"),QMessageBox::Ok);
//    connect(serial,SIGNAL(readyRead()),this,SLOT(readData()));
}

QByteArray PRINT::change(QString a,int style)
{
    int i=0;
    QByteArray data;
    qDebug()<<"The change str is"<<a;
    if(a == "不化整")
    {
        data.append(0xb2);
        data.append(0xbb);
        data.append(0xbb);
        data.append(0xaf);
        data.append(0xd5);
        data.append(0xfb);
    }
    else
    {
        for (; i < a.length(); i++)
        {
            if(a[i] == '.')
            {
                data.append(0x2e);
            }
            else if( (a[i]>='0' && a[i]<='9') || (a[i] >= 'A' && a[i] <= 'Z') || (a[i] >= 'a' && a[i] <= 'z'))
            {
                data.append(a[i].toAscii());
            }
            else if(a[i] == '-')
            {
                data.append(0x2d);
            }
            else if(a[i] == '/')
            {
                data.append(0x2f);
            }
            else if(a[i] == '√')
            {
                data.append(0xa1);
                data.append(0xcc);
            }
        }
    }
    if(style == 2 && data.length() < 6 )
    {
        i=data.length();
        while(i < 6)
        {
            data.append(0x20);
            i++;
        }
    }
    if(style == 3 && data.length() < 18 )
    {
        i=data.length();
        while(i < 18)
        {
            data.append(0x20);
            i++;
        }
    }
    qDebug()<<"data length is"<<data.length();
    return data;
}

void PRINT::select(QStringList list)
{
    int i=0,j=7,k=17;
    bh = list.at(0);
    xinh = list.at(1);
    bb = list.at(2);
    jd = list.at(3);
    jd = jd.simplified();
    glys = list.at(4);
    edfh = list.at(5);
    xxfh = list.at(6);
    for(i=0;i<8;i++)
    {
        fhed[i] = list.at(j);
        fhxx[i] = list.at(k);
        j++;
        k++;
    }
    fhed[8] = list.at(15);
    fhed[9] = list.at(16);

    cstime = list.at(25);
    jdjg = list.at(26);

    hanzitest();
}

void PRINT::getTime()
{
    QDateTime dt;
    QTime time;
    QDate date;

    dt.setTime(time.currentTime());
    dt.setDate(date.currentDate());

    QString currentDate = dt.toString("yyyy-MM-dd  hh:mm:ss");
    timesplit(currentDate);
}

void PRINT::timesplit(QString time_s)
{
    int i = 0;
    QString time="";
    QByteArray time1;
    for(i=0;i<time_s.length();i++)
    {
        if(time_s[i] != '-' && time_s[i] != ' ')
        {
            time.append(time_s[i]);
        }
    }
    qDebug()<<"The cstime is"<<time;
    time1 = change(time,1);
    year.append(time1.left(4));
    time1=time1.mid(4);

    mon.append(time1.left(2));
    time1=time1.mid(2);

    dd.append(time1.left(2));
    time1=time1.mid(2);

    hour.append(time1.left(2));
    time1=time1.mid(2);

    min.append(time1.left(2));
    time1=time1.mid(2);

    second.append(time1.left(2));
    time1=time1.mid(2);
}

void PRINT::print(unsigned char ch)
{
    QByteArray data;
    data.append(ch);
    serial->write(data);

    QTime t;
    t.start();

//    qDebug()<<"print"<<hex<<(int)ch;
    while(t.elapsed() < 2) QCoreApplication::processEvents();
}

void PRINT::hanzitest()
{
    unsigned char i;
    QByteArray shuju;
    getTime();
    print(0x1b);print(0x40);	 /*初始化打印机*/

    print(0x1b);print(0x20);print(0x00);             //间距
    print(0x1b);print(0x38);print(0x04);   	/*调用16*16点阵汉字，24点阵为24*24*/
    for(i=0;i<30;i++)
    {
        print(0x2a);
    }
    print(0x1b);print(0x66);print(0x01);print(0x01);//2行

    print(0xb4);print(0xf2);print(0xd3);print(0xa1);print(0xca);print(0xb1);print(0xbc);print(0xe4); //打印时间
    for(i=0;i<year.length();i++)
    {
        print(year[i]);
    }
    print(0xc4);print(0xea);//2015年
    for(i=0;i<mon.length();i++)
    {
        print(mon[i]);
    }
    print(0xd4);print(0xc2);    //09月
    for(i=0;i<dd.length();i++)
    {
        print(dd[i]);
    }
    print(0xc8);print(0xd5); //14日
    for(i=0;i<hour.length();i++)
    {
        print(hour[i]);
    }
    print(0x3a); //11:
    for(i=0;i<min.length();i++)
    {
        print(min[i]);
    }
    print(0x3a); //26:
    for(i=0;i<second.length();i++)
    {
        print(second[i]);
    }
    year.clear();
    mon.clear();
    dd.clear();
    hour.clear();
    min.clear();
    second.clear();
    print(0x1b);print(0x66);print(0x01);print(0x01);//2行

    //120%
    print(0x31);print(0x32);print(0x30);print(0x25);
    print(0x1b);print(0x66);print(0x00);print(0x00);//空格
    print(0x7c);
    shuju = change(fhed[4],2);
    for(i=0;i<shuju.length();i++)
    {
        print(shuju[i]);
    }
    shuju.clear();

    print(0x1b);print(0x66);print(0x00);print(0x01);//空格
    shuju = change(fhed[9],2);
    for(i=0;i<shuju.length();i++)
    {
        print(shuju[i]);
    }
    shuju.clear();

    print(0x1b);print(0x66);print(0x00);print(0x00);//空格
    print(0x7c);

//    shuju = change(fhxx[6],2);
//    for(i=0;i<shuju.length();i++)
//    {
//        print(shuju[i]);
//    }
//    shuju.clear();

//    print(0x1b);print(0x66);print(0x00);print(0x01);//空格

//    shuju = change(fhxx[7],2);
//    for(i=0;i<shuju.length();i++)
//    {
//        print(shuju[i]);
//    }
//    shuju.clear();

    //100%
    print(0x1b);print(0x66);print(0x01);print(0x00);//空行
    print(0x31);print(0x30);print(0x30);print(0x25);
    print(0x1b);print(0x66);print(0x00);print(0x00);//空格
    print(0x7c);
    shuju = change(fhed[3],2);
    for(i=0;i<shuju.length();i++)
    {
        print(shuju[i]);
    }
    shuju.clear();

    print(0x1b);print(0x66);print(0x00);print(0x01);//空格

    shuju = change(fhed[8],2);
    for(i=0;i<shuju.length();i++)
    {
        print(shuju[i]);
    }
    shuju.clear();

    print(0x1b);print(0x66);print(0x00);print(0x00);//空格
    print(0x7c);

    shuju = change(fhxx[3],2);
    for(i=0;i<shuju.length();i++)
    {
        print(shuju[i]);
    }
    shuju.clear();

    print(0x1b);print(0x66);print(0x00);print(0x01);//空格

    shuju = change(fhxx[7],2);
    for(i=0;i<shuju.length();i++)
    {
        print(shuju[i]);
    }
    shuju.clear();

    //20%
    print(0x1b);print(0x66);print(0x01);print(0x00);//空行
    print(0x32);print(0x30);print(0x25);
    print(0x1b);print(0x66);print(0x00);print(0x01);//空格
    print(0x7c);
    shuju = change(fhed[2],2);
    for(i=0;i<shuju.length();i++)
    {
        print(shuju[i]);
    }
    shuju.clear();

    print(0x1b);print(0x66);print(0x00);print(0x01);//空格

    shuju = change(fhed[7],2);
    for(i=0;i<shuju.length();i++)
    {
        print(shuju[i]);
    }
    shuju.clear();

    print(0x1b);print(0x66);print(0x00);print(0x00);//空格
    print(0x7c);

    shuju = change(fhxx[2],2);
    for(i=0;i<shuju.length();i++)
    {
        print(shuju[i]);
    }
    shuju.clear();
    print(0x1b);print(0x66);print(0x00);print(0x01);//空格

    shuju = change(fhxx[6],2);
    for(i=0;i<shuju.length();i++)
    {
        print(shuju[i]);
    }
    shuju.clear();

    //5%
    print(0x1b);print(0x66);print(0x01);print(0x00);//空行
    print(0x35);print(0x25);
    print(0x1b);print(0x66);print(0x00);print(0x02);//空格
    print(0x7c);

    shuju = change(fhed[1],2);
    for(i=0;i<shuju.length();i++)
    {
        print(shuju[i]);
    }
    shuju.clear();

    print(0x1b);print(0x66);print(0x00);print(0x01);//空格

    shuju = change(fhed[6],2);
    for(i=0;i<shuju.length();i++)
    {
        print(shuju[i]);
    }
    shuju.clear();

    print(0x1b);print(0x66);print(0x00);print(0x00);//空格
    print(0x7c);

    shuju = change(fhxx[1],2);
    for(i=0;i<shuju.length();i++)
    {
        print(shuju[i]);
    }
    shuju.clear();

    print(0x1b);print(0x66);print(0x00);print(0x01);//空格

    shuju = change(fhxx[5],2);
    for(i=0;i<shuju.length();i++)
    {
        print(shuju[i]);
    }
    shuju.clear();

    //1%
    print(0x1b);print(0x66);print(0x01);print(0x00);//空行
    print(0x31);print(0x25);
    print(0x1b);print(0x66);print(0x00);print(0x02);//空格
    print(0x7c);

    shuju = change(fhed[0],2);
    for(i=0;i<shuju.length();i++)
    {
        print(shuju[i]);
    }
    shuju.clear();

    print(0x1b);print(0x66);print(0x00);print(0x01);//空格

    shuju = change(fhed[5],2);
    for(i=0;i<shuju.length();i++)
    {
        print(shuju[i]);
    }
    shuju.clear();

    print(0x1b);print(0x66);print(0x00);print(0x00);//空格
    print(0x7c);

    shuju = change(fhxx[0],2);
    for(i=0;i<shuju.length();i++)
    {
        print(shuju[i]);
    }
    shuju.clear();

    print(0x1b);print(0x66);print(0x00);print(0x01);//空格

    shuju = change(fhxx[4],2);
    for(i=0;i<shuju.length();i++)
    {
        print(shuju[i]);
    }
    shuju.clear();

    print(0x1b);print(0x66);print(0x01);print(0x00);//空行
    print(0x1b);print(0x66);print(0x00);print(0x04);//空格
    print(0x7c);
    print(0x1b);print(0x66);print(0x00);print(0x01);//空格
    print(0x66);print(0x28);print(0x25);print(0x29); //f(%)
    print(0x1b);print(0x66);print(0x00);print(0x03);//空格
    print(0xa6);print(0xc4);print(0x28);print(0x27);print(0x29); //δ(')
    print(0x1b);print(0x66);print(0x00);print(0x00);//空格
    print(0x7c);
    print(0x1b);print(0x66);print(0x00);print(0x01);//空格
    print(0x66);print(0x28);print(0x25);print(0x29); //f(%)
    print(0x1b);print(0x66);print(0x00);print(0x03);//空格
    print(0xa6);print(0xc4);print(0x28);print(0x27);print(0x29); //δ(')

    print(0x1b);print(0x66);print(0x01);print(0x00);//空行
    print(0x1b);print(0x66);print(0x00);print(0x04);//空格
    //额定

    print(0x7c);
    print(0x1b);print(0x66);print(0x00);print(0x00);
    print(0xb6);print(0xee);print(0xb6);print(0xa8);
    print(0x28);

    shuju = change(edfh,1);
    for(i=0;i<shuju.length();i++)
    {
        print(shuju[i]);
    }
    shuju.clear();

    print(0x1b);print(0x66);print(0x00);print(0x02);
//    print(0x56);print(0x41);
    print(0x29);
    print(0x1b);print(0x66);print(0x00);print(0x01);
    //下限
    print(0x7c);
    print(0x1b);print(0x66);print(0x00);print(0x00);
    print(0xcf);print(0xc2);print(0xcf);print(0xde);
    print(0x28);

    shuju = change(xxfh,1);
    for(i=0;i<shuju.length();i++)
    {
        print(shuju[i]);
    }
    shuju.clear();

    print(0x1b);print(0x66);print(0x00);print(0x02);
//    print(0x56);print(0x41);
    print(0x29);

    print(0x1b);print(0x66);print(0x01);print(0x01);//空一行

    for(i=0;i<30;i++)
    {
        print(0x2d);
    }

    print(0x1b);print(0x66);print(0x01);print(0x01);//空一行
//    timesplit(cstime);
    year = change(cstime.left(4),1);
    cstime = cstime.mid(5);
    mon = change(cstime.left(2),1);
    cstime = cstime.mid(3);
    dd = change(cstime.left(2),1);

    print(0xb2);print(0xe2);print(0xca);print(0xd4);print(0xca);print(0xb1);print(0xbc);print(0xe4);//测试时间：
    for(i=0;i<year.length();i++)
    {
        print(year[i]);
    }
    print(0xc4);print(0xea);//2015年
    for(i=0;i<mon.length();i++)
    {
        print(mon[i]);
    }
    print(0xd4);print(0xc2);    //09月
    for(i=0;i<dd.length();i++)
    {
        print(dd[i]);
    }
    print(0xc8);print(0xd5); //14日
//    for(i=0;i<hour.length();i++)
//    {
//        print(hour[i]);
//    }
//    print(0x3a); //11:
//    for(i=0;i<min.length();i++)
//    {
//        print(min[i]);
//    }
//    print(0x3a); //26:
//    for(i=0;i<second.length();i++)
//    {
//        print(second[i]);
//    }
    year.clear();
    mon.clear();
    dd.clear();
    hour.clear();
    min.clear();
    second.clear();

    print(0x1b);print(0x66);print(0x01);print(0x00);//空一行

    print(0xb9);print(0xa6);print(0xc2);print(0xca);print(0xd2);print(0xf2);print(0xcb);print(0xd8);print(0x3a); //功率因数：
    shuju = change(glys,1);
    for(i=0;i<shuju.length();i++)
    {
        print(shuju[i]);
    }
    shuju.clear();

    print(0x1b);print(0x66);print(0x01);print(0x00);//空一行

    if(jd != "不化整")
    {
        print(0xb2);print(0xe2);print(0xca);print(0xd4);print(0xbd);print(0xe1);print(0xb9);print(0xfb);print(0x3a);//精度：
        if(jdjg == "合格")
        {
            print(0xba);print(0xcf);print(0xb8);print(0xf1);
        }
        else if(jdjg == "不合格")
        {
            print(0xb2);print(0xbb);print(0xba);print(0xcf);print(0xb8);print(0xf1);
        }

        print(0x1b);print(0x66);print(0x01);print(0x00);//空一行
    }

    print(0xbe);print(0xab);print(0xb6);print(0xc8);print(0x3a);//精度：

    shuju = change(jd,1);
    for(i=0;i<shuju.length();i++)
    {
        print(shuju[i]);
    }
    shuju.clear();

    print(0x1b);print(0x66);print(0x01);print(0x00);//空一行

    print(0xb1);print(0xe4);print(0xb1);print(0xc8);print(0x3a);//变比	：
    shuju = change(bb,1);
    for(i=0;i<shuju.length();i++)
    {
        print(shuju[i]);
    }
    shuju.clear();

    print(0x1b);print(0x66);print(0x01);print(0x00);//空一行

    print(0xd0);print(0xcd);print(0xba);print(0xc5);print(0x3a);//型号	：
    shuju = change(xinh,1);
    for(i=0;i<shuju.length();i++)
    {
        print(shuju[i]);
    }
    shuju.clear();

    print(0x1b);print(0x66);print(0x01);print(0x00);//空一行
    print(0xb1);print(0xe0);print(0xba);print(0xc5);print(0x3a); //编号：
    shuju = change(bh,3);
    for(i=0;i<shuju.length();i++)
    {
        print(shuju[i]);
    }
    shuju.clear();

    print(0x1b);print(0x66);print(0x01);print(0x01);//空一行
    print(0x1b);print(0x66);print(0x00);print(0x09);//空一行

    print(0x35);print(0x39);print(0x30);print(0x43);print(0x2d);print(0x32);
    print(0xb2);print(0xe2);print(0xca);print(0xd4);print(0xb1); print(0xa8);print(0xb8);print(0xe6);

    print(0x1b);print(0x66);print(0x01);print(0x01);//空一行

    for(i=0;i<30;i++)
    {
        print(0x2a);
    }
    print(0x0d);
    print(0x0d);
    print(0x0d);
    qDebug()<<"hanzi test, bye!";
    shuju.clear();
    serial->waitForBytesWritten(1);
    serial->close();
}

